﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Assignment1
{
    public partial class CinemaLocatorForm : Form
    {
        public CinemaLocatorForm()
        {
            InitializeComponent();
        }

        // Search button clicked
        private void _searchButton_Click(object sender, EventArgs e)
        {
            int indexA, indexB;
            int radius = 5;

            // check for radio button checked
            if (_radio10mi.Checked == true)
            {
                radius = 10;
            }
            else if (_radio15mi.Checked == true)
            {
                radius = 15;
            }

            // create teh soap service
            proxyTheaterLocator.MovieInformationSoapClient theater =
                new proxyTheaterLocator.MovieInformationSoapClient("MovieInformationSoap");

            // get the theater list
            proxyTheaterLocator.Theater[] theaterDS =
                theater.GetTheatersAndMovies(_zipCode.Text.Trim(), radius);

            // get the results
            _searchResults.Text = "";
            try
            {
                // check if any records exist
                if (theaterDS.Length == 0)
                {
                    _searchResults.AppendText(
                        "No theaters found for the zip code: " + _zipCode.Text +
                        " within a " + radius.ToString() + "mi radius.\r\n");
                }
                foreach (proxyTheaterLocator.Theater theater_result in theaterDS)
                {
                    for (int i = 0; i < theater_result.Movies.Length - 1; ++i)
                    {
                        // add teh movie name
                        _searchResults.AppendText(theater_result.Movies[i].Name.ToString() + "\n");

                        try
                        {
                            // add the movie rating
                            _searchResults.AppendText(theater_result.Movies[i].Rating.ToString() + "\n");
                        }
                        catch(NullReferenceException){}

                        try
                        {
                            // add the movie running time
                            _searchResults.AppendText(theater_result.Movies[i].RunningTime.ToString() + "\n");
                        }
                        catch(NullReferenceException){}

                        try
                        {
                            // add the show times
                            _searchResults.AppendText(theater_result.Movies[i].ShowTimes.ToString() + "\n");
                        }
                        catch(NullReferenceException){}

                        _searchResults.AppendText( "\n\n");
                    }
                }
            }
            catch (NullReferenceException) { }
        }
    }
}
